﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace DataBaseConnection
{
    /// <summary>
    /// 数据库类型
    /// </summary>
    public enum DataBaseType
    {
        /// <summary>
        /// 微软本地小型数据库
        /// </summary>
        ACCESS,
        /// <summary>
        /// 微软中型数据库服务
        /// </summary>
        MSSQL
    }
    
    /// <summary>
    /// 主线程调用选择不同的数据库
    /// </summary>
    public partial class CSelectDataBase
    {
        /// <summary>
        /// 选择一个数据库类型,获取配制文件里的默认的连接字符串"DefaultConnection"
        /// </summary>
        public static PConnOrder GetDataBase()
        {
            string dbname = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["DefaultKey"]);
            if ( dbname == null || dbname == string.Empty) { dbname = "DefaultConnection"; }
            return GetDataBase(dbname);
        }
        /// <summary>
        /// 选择一个数据库类型
        /// </summary>
        /// <param name="_ConnKeyName">配制文件 connectionStrings.name</param>
        /// <returns></returns>
        public static PConnOrder GetDataBase(string _ConnKeyName)
        {
            DataBaseType dbt = DataBaseType.MSSQL;
            string connString = GetConnectionStrings(_ConnKeyName, ref dbt);
            return GetDataBase(dbt, connString);
        }
        /// <summary>
        /// 选择一个数据库类型
        /// </summary>
        /// <param name="_DBType">数据库类型</param>
        /// <param name="_ConnectionString">连接字符串</param>
        /// <returns></returns>
        public static PConnOrder GetDataBase(DataBaseType _DBType, string _ConnectionString)
        {
            PConnOrder newConnOrder = null;

            switch (_DBType)
            {
                case DataBaseType.ACCESS:
                    newConnOrder = new CMSAccess();
                    break;
                case DataBaseType.MSSQL:
                    newConnOrder = new CMSSQL();
                    break;
            }

            //设置连接字符串
            newConnOrder.SetConnectionText = _ConnectionString;
            return newConnOrder;
        }

        /// <summary>
        /// 获取匹配的数据库类型
        /// </summary>
        /// <param name="providerName">配制文件 connectionStrings.providerName</param>
        /// <returns></returns>
        public static DataBaseType MatchType(string providerName)
        {
            DataBaseType dbt = DataBaseType.MSSQL;
            switch (providerName.ToUpper())
            {
                case "SYSTEM.DATA.SQLCLIENT":
                    dbt = DataBaseType.MSSQL;
                    break;
                case "SYSTEM.DATA.OLEDB":
                    dbt = DataBaseType.ACCESS;
                    break;                
            }
            return dbt;
        }

        /// <summary>
        /// 返回配制文件里指定的字符串
        /// </summary>
        /// <param name="Name">关键字</param>
        /// <returns>连接字符串</returns>
        public static string GetConnectionStrings(string Name, ref DataBaseType _DBType)
        {
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings[Name];
            
            if (connString != null)
            {
                _DBType = MatchType(connString.ProviderName);
                return connString.ConnectionString;
            }

            throw new Exception("连接字符串为空");
        }
    }
}
